package tcpandudp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * socket通信是基于tcp的
 */
public class InternetTest {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getByName("localhost"), 9000);
        OutputStream outputStream = socket.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(new File("file/src/1.jpeg"));
        System.out.println(fileInputStream);
        byte[] bytes = new byte[1024];
        int len;
        // 先从磁盘中读取文件到内存
        while ((len=fileInputStream.read(bytes))!=-1) {
            // 再从内存写文件到流中
            outputStream.write(bytes,0,len);
        }
        fileInputStream.close();
        outputStream.close();
        socket.close();
    }
}
